<?php
/**
 * @ClassName: Merchant
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-20 16:25
 * @Version: V1.0.0
 */

namespace app\api\controller\water;

use app\api\controller\Api;
use app\common\model;

class Merchant extends Api
{
    /**
     * 商户申请
     * @return \think\response\Json
     */
    function apply()
    {
        $data = input();

        $add_data = [
            'member_id' => $this->user_id,
            'store_type' => $data['store_type'],
            'merchant_type' => $data['merchant_type'],
            'merchant_name' => $data['merchant_name'],
            'store_name' => $data['store_name'],
            'contact_name' => $data['contact_name'],
            'contact_phone' => $data['contact_phone'],
            'email' => $data['email'],
            'street_address' => $data['street_address'],
            'unity_category_id' => $data['unity_category_id'],
            'license_id' => $data['license_id'],
            'license_name' => $data['license_name'],
            'create_time' => date_now(),
        ];

        $res = model\MerchantApply::create($add_data);
        if (!$res) {
            return json(['code' => 0, 'msg' => '申请失败，请联系平台客户']);
        }

        return json(['code' => 1, 'msg' => '申请成功']);
    }

    /**
     * 申请商户列表
     * @return \think\response\Json
     */
    function getList()
    {
        $type = input('type', 0);
        $skip = input('skip', 0);
        $limit = input('limit', 10);

        $where = [];
        $where[] = ['member_id', '=', $this->user_id];

        switch ($type) {
            case 1:
                // 待审核
                $where[] = ["status", '=', 0];
                break;
            case 2:
                // 不通过
                $where[] = ["status", '=', 2];
                break;
            case 3:
                // 通过
                $where[] = ["status", '=', 1];
                break;
            default:
                // 全部
                break;
        }

        $list = model\MerchantApply::where($where)->limit($skip, $limit)->select();

        $result = [
            'list' => $list,
            'is_more' => count($list) >= $limit
        ];

        return json(["code" => 1, "msg" => "获取成功", "data" => $result]);
    }

    /**
     * 保存申请商户信息
     * @return \think\response\Json
     */
    function save()
    {
        $data = input();

        $up_data = [
            'member_id' => $this->user_id,
            'store_type' => $data['store_type'],
            'merchant_type' => $data['merchant_type'],
            'merchant_name' => $data['merchant_name'],
            'store_name' => $data['store_name'],
            'contact_name' => $data['contact_name'],
            'contact_phone' => $data['contact_phone'],
            'email' => $data['email'],
            'street_address' => $data['street_address'],
            'unity_category_id' => $data['unity_category_id'],
            'license_id' => $data['license_id'],
            'license_name' => $data['license_name'],
            'status' => $data['status'],
            'update_time' => date_now(),
        ];

        $res = model\MerchantApply::where('id', $data['id'])->update($up_data);
        if (!$res) {
            return json(['code' => 0, 'msg' => '保存失败，请联系平台客户']);
        }

        return json(['code' => 1, 'msg' => '保存成功']);
    }

    /**
     * 申请商户信息详情
     * @return \think\response\Json
     */
    function detail()
    {
        $id = input('id');

        $data = model\MerchantApply::get($id);

        return json(['code' => 1, 'msg' => 'ok', 'data' => $data]);
    }

}